package cn.yerly.sakura.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import cn.yerly.sakura.domain.util.CustomLocalDateSerializer;
import cn.yerly.sakura.domain.util.ISO8601LocalDateDeserializer;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.joda.time.LocalDate;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A TradeItem.
 */
@Entity
@Table(name = "TRADEITEM")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName="tradeitem")
public class TradeItem implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Min(value = 0)
    @Column(name = "item_price", precision=10, scale=2, nullable = false)
    private BigDecimal itemPrice;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    @JsonSerialize(using = CustomLocalDateSerializer.class)
    @JsonDeserialize(using = ISO8601LocalDateDeserializer.class)
    @Column(name = "ctime", nullable = false)
    private LocalDate ctime;

    @Column(name = "disabled")
    private Boolean disabled;

    @ManyToOne
    private User user;

    @ManyToOne
    private Trade trade;

    @OneToOne
    private Item item;

    @OneToOne
    private ItemSpec itemSpec;

    @OneToOne
    private ItemInfo itemInfo;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public BigDecimal getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(BigDecimal itemPrice) {
        this.itemPrice = itemPrice;
    }

    public LocalDate getCtime() {
        return ctime;
    }

    public void setCtime(LocalDate ctime) {
        this.ctime = ctime;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Trade getTrade() {
        return trade;
    }

    public void setTrade(Trade trade) {
        this.trade = trade;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public ItemSpec getItemSpec() {
        return itemSpec;
    }

    public void setItemSpec(ItemSpec itemSpec) {
        this.itemSpec = itemSpec;
    }

    public ItemInfo getItemInfo() {
        return itemInfo;
    }

    public void setItemInfo(ItemInfo itemInfo) {
        this.itemInfo = itemInfo;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TradeItem tradeItem = (TradeItem) o;

        if ( ! Objects.equals(id, tradeItem.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "TradeItem{" +
                "id=" + id +
                ", itemPrice='" + itemPrice + "'" +
                ", ctime='" + ctime + "'" +
                ", disabled='" + disabled + "'" +
                '}';
    }
}
